package com.ddyx.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ddyx.entity.MessageForm;
import com.ddyx.entity.WxUser;
import com.ddyx.mapper.MessageFormMapper;
import com.ddyx.mapper.WxUserMapper;
import com.ddyx.service.IMessageFormService;
import com.ddyx.util.DateUtil;
import com.ddyx.util.EventTag;
import com.ddyx.util.MyBeanUtils;
import com.ddyx.util.PropertiesUtil;
import com.ddyx.util.RequestForm;
import com.ddyx.util.ResponseForm;
import com.ddyx.util.template.KeyWord;
import com.ddyx.util.template.TemplateMessageParam;
import com.ddyx.util.template.TemplateMessageUtil;
import com.fasterxml.jackson.databind.ObjectMapper;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class MessageFormServiceImpl implements IMessageFormService {

	@Resource
	MessageFormMapper msgFormMapper;
	@Resource
	WxUserMapper wxUserMapper;
	
	private static ObjectMapper jsonMapper = new ObjectMapper();

	/*
	 * 保存formid
	 */
	@Transactional
	@SuppressWarnings("unchecked")
	@Override
	public void saveFormIds(RequestForm param) {
		ResponseForm result = new ResponseForm();
		if (param.getData() != null) {
			List<Map> FormIds = (List<Map>) param.getData();
//			System.out.println("--");
//			MessageForm bean = new MessageForm();
			for (Map map : FormIds) {
//				MyBeanUtils.populate(bean, map);
//				if (!bean.getFormId().equals("the formId is a mock one") && StringUtils.isNotBlank(bean.getFormId())) {// 如果formid是虚拟端发送
//					msgFormMapper.insertSelective(bean);
//				}
				map.put("createTime", DateUtil.toDate(map.get("createTime")));
				if (map.containsKey("formId")&&!map.containsValue("the formId is a mock one")) {// 如果formid是虚拟端发送
					msgFormMapper.saveFormId(map);
				}
			}
		}
	}

	/*
	 * 发送红包通知
	 */
	@SuppressWarnings("unchecked")
	@Override
	@Transactional
	public ResponseForm hbActivityNotify(RequestForm param) {
		ResponseForm result = new ResponseForm();
		if (param.getData() == null || param.getData() == "") {
			Map<String, Object> paramMap = (Map<String, Object>) param.getData();
			TemplateMessageParam tmpMsgParam = getHbTmp();
			try {
				List<Map<String, Object>> forms = msgFormMapper.hbActivityNotify(paramMap);
				for (Map<String, Object> map : forms) {
					tmpMsgParam.setForm_id((String) map.get("formId"));
					tmpMsgParam.setTouser((String) map.get("openId"));

					String json = jsonMapper.writeValueAsString(tmpMsgParam);

					TemplateMessageUtil.sendTmpMsg(json);

				}
				for (Map<String, Object> map : forms) {
					msgFormMapper.deleteByPrimaryKey(map.get("formId"));
				}

			} catch (Exception e) {
				e.printStackTrace();
				result.setStatus(false);
				result.setMessage("操作有误");
				log.error("操作有误");
			}
			return result;
		}
		return null;
	}

	/**
	 * 模版消息处理
	 * 
	 * @return
	 */
	private static TemplateMessageParam getHbTmp() {
		TemplateMessageParam tmpMsgParam = new TemplateMessageParam();
		List list = new ArrayList<>();
		list.add(new KeyWord(PropertiesUtil.getProperty(EventTag.HB_THEME), EventTag.RED));
		list.add(new KeyWord(PropertiesUtil.getProperty(EventTag.HB_INFO), EventTag.BLACK));
		list.add(new KeyWord(PropertiesUtil.getProperty(EventTag.HB_TIME), EventTag.RED));
		list.add(new KeyWord(PropertiesUtil.getProperty(EventTag.HB_NAME), EventTag.BLACK));
		Map<String, Object> keyword = new HashMap<>();
		int i = 1;
		for (Object o : list) {
			keyword.put("keyword" + i, o);
			i++;
		}
		tmpMsgParam.setTemplate_id(PropertiesUtil.getProperty(EventTag.HBTEMP));
		tmpMsgParam.setEmphasis_keyword("keyword" + 1 + ".DATA");
		// tmpMsgParam.setPage();
		tmpMsgParam.setData(keyword);
		return tmpMsgParam;

	}
}
